<?php
/**
 * 公司基本信息
 */

namespace app\admin\controller;


use app\admin\model\Company as CompanyModel;
use app\admin\model\CompanyAccount;
use app\admin\model\Region;
use app\common\controller\AdminController;
use think\Exception;
use think\facade\Request;

class company extends AdminController {

    public $resubmitLock = ['addpost', 'editpost'];

    /**
     * 权限配置
     * 1查看权限、2管理权限、4导入权限、
     * @var array
     */
    protected $levelConfig = [
        1 => ['addget', 'childregion', 'getlist', 'editget'],
        2 => ['addpost', 'editpost', 'deleted', 'resetpwd', 'uploadlogo'],
        4 => [],
        8 => []
    ];

    /**
     * 添加获取
     * @return false|string
     */
    public function addGet() {
        try {
            $params = $this->checkParams('');
            if ($params === false) return $this->resErr();
            $city_codes = Region::myCityCodes($params);
            $house_city = db('region')
                ->where('city_code', 'in', $city_codes)
                ->field('name,city_code')
                ->select();
            $rank1 = Region::getDatas([['rank', '=', 1]], 'name,id,city_code');//省
            return $this->resData('success', compact('rank1', 'house_city'));
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 添加提交
     * @return false|string
     */
    public function addPost() {
        try {
            $params = $this->checkParams('f_name,f_phone,name,logo,app_logo,province_name,city_name,city_code,area_name,address,applets_id,appid,secret,agent_num,applets_code,wechat_appid,wechat_secret,default_city_code');
            if ($params === false) return $this->resErr();
            $res = CompanyModel::post($params);
            if (!$res) {
                return $this->resErr();
            } else {
                return $this->resData('创建成功', []);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 省市区联动
     * @return false|string
     */
    public function childRegion() {
        try {
            $params = $this->checkParams('id');
            if ($params === false) return $this->resErr();
            $list = Region::getDatas([['pid', '=', $params['id']]], 'name,id,city_code');//市或者区
            return $this->resData('success', $list);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 公司列表
     * @return false|string
     */
    public function getList() {
        try {
            $params = $this->checkParams('');
            if ($params === false) return $this->resErr();
            $list = CompanyModel::getList($params);
            return $this->resData('success', $list);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 编辑获取
     * @return false|string
     */
    public function editGet() {
        try {
            $params = $this->checkParams('id');
            if ($params === false) return $this->resErr();
            $res = CompanyModel::getInfo($params);
            if (!$res) return $this->resErr();
            else return $this->resData('success', $res);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 编辑提交
     * @return false|string
     */
    public function editPost() {
        try {
            $params = $this->checkParams('id,name,province_name,city_name,area_name,city_code,address,agent_num,f_name,logo,app_logo,default_city_code');
            if ($params === false) return $this->resErr();
            $res = CompanyModel::post($params);
            if (!$res) return $this->resErr();
            else return $this->resData('修改成功', []);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除
     * @return false|string
     */
    public function deleted() {
        try {
            $params = $this->checkParams('id');
            if ($params === false) return $this->resErr();
            $res = CompanyModel::del($params);
            if (!$res) return $this->resErr();
            else return $this->resData('删除成功', []);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 重置密码
     * @return false|string
     */
    public function resetPwd() {
        try {
            $params = $this->checkParams('id');
            if ($params === false) return $this->resErr();
            $res = CompanyAccount::resetPwd($params);
            if (!$res) return $this->resErr();
            return $this->resData('success', ['password' => $res]);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 上传Logo
     * @return false|string
     */
    function uploadLogo() {
        try {
            $params = $this->checkParams('');
            if ($params === false) return $this->resErr();
            $file = request()->file('logo');
            $file_header = config('global.file_dir');
            $file_domain = config('global.file_domain');
            $info = $file->validate(['size' => 5242880, 'ext' => 'jpg,png,gif,jpeg'])->move($file_header . 'company_logo/');
            if ($info) {
                $save_file = $file_domain . 'company_logo/' . $info->getSaveName();
                return $this->resData('success', ['url' => $save_file]);
            } else {
                // 上传失败获取错误信息
                echo $file->getError();
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

}
